package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.apis.wemedia.IWemediaClient;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.AdminAuthDto;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.user.pojos.ApUserRealname;
import com.heima.model.wemedia.dtos.WmUserDto;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.user.mapper.AdminAuthMapper;
import com.heima.user.service.AdminAuthService;
import com.heima.user.service.ApUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
@Transactional
public class AdminAuthServiceImpl extends ServiceImpl<AdminAuthMapper, ApUserRealname> implements AdminAuthService {

    /**
     * 分页查询认证列表
     * @param dto
     * @return
     */
    @Override
    public ResponseResult findList(AdminAuthDto dto) {
        // 需求
        // 1. 可根据审核状态条件查询
        // 2. 需要分页查询

        // 1. 检查参数
        dto.checkParam();

        // 2. 根据审核状态条件分页查询
        IPage page = new Page(dto.getPage(), dto.getSize());
        LambdaQueryWrapper<ApUserRealname> query = new LambdaQueryWrapper<>();
        // 2.1 根据审核状态条件查询
        query.like(dto.getStatus()!=null, ApUserRealname::getStatus, dto.getStatus());
        // 2.2 按照创建时间倒序查询
        query.orderByDesc(ApUserRealname::getCreatedTime);
        // 2.3 查询结果
        page(page, query);

        // 3. 返回结果
        PageResponseResult result = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());
        result.setData(page.getRecords());
        return result;
    }

    @Autowired
    private IWemediaClient wemediaClient;
    @Autowired
    private ApUserService apUserService;

    /**
     * 审核通过
     * @param dto
     * @return
     */
    @Override
    public ResponseResult authPass(AdminAuthDto dto) {
        // 需求
        // 1. 用户通过审核后需要开通自媒体账号(账号的用户名和密码和app一致)
        ApUserRealname apUserRealname = new ApUserRealname();
        apUserRealname.setId(dto.getId());
        apUserRealname.setStatus((short) 9);
        apUserRealname.setUpdatedTime(new Date());
        updateById(apUserRealname);

        // 2. 开通自媒体账号
        ApUser apUser = apUserService.getById(dto.getId());
        WmUserDto wmUserDto = new WmUserDto();
        BeanUtils.copyProperties(apUser, wmUserDto);
        ResponseResult responseResult = wemediaClient.saveUser(wmUserDto);
        if(!responseResult.getCode().equals(200)){
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 审核失败
     * @param dto
     * @return
     */
    @Override
    public ResponseResult authFail(AdminAuthDto dto) {
        ApUserRealname apUserRealname = new ApUserRealname();
        apUserRealname.setId(dto.getId());
        apUserRealname.setStatus((short) 2);
        apUserRealname.setReason(dto.getMsg());
        apUserRealname.setUpdatedTime(new Date());
        updateById(apUserRealname);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
